package com.juggernaut;

import java.io.IOException;

import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.CloudSolrClient;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.request.CollectionAdminRequest;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.params.ModifiableSolrParams;

/**
 * Hello solr!
 * 
 */
public class Test {

	public static void main(String[] args) throws SolrServerException, IOException {
		solrCreateIndex();
	}

	// standalone模式的连接
	public static void singleNodeQuery() throws SolrServerException, IOException {
		String urlString = "http://172.17.149.196:8983/solr/techproducts";
		SolrClient solr = new HttpSolrClient(urlString);
		ModifiableSolrParams params = new ModifiableSolrParams();
		params.set("q", "*:*");
		params.set("fl", "id,name,price,_version_");
		params.set("facet", true);
		params.set("facet.field", "name");

		QueryResponse res = solr.query(params);
		System.out.println(res.getFacetFields());
		SolrDocumentList list = res.getResults();
		System.out.println("total:" + list.getNumFound());
		System.out.println("size:" + list.size());
		for (SolrDocument doc : list) {
			System.out.println(doc);
		}

		solr.close();
	}

	// 添加和更新
	public static void singleNodeUpsert() throws SolrServerException, IOException {
		String urlString = "http://172.17.149.196:8983/solr/techproducts";
		HttpSolrClient solr = new HttpSolrClient(urlString);
		SolrInputDocument document = new SolrInputDocument();

		String[] cat = { "3", "4", "5" };
		document.addField("id", "552199");
		document.addField("name", "Gouda cheese wheel11133");
		document.addField("price", "49.99");
		document.addField("cat", cat);

		UpdateResponse response = solr.add(document);
		System.out.println(response);

		// Remember to commit your changes!
		solr.commit();
		solr.close();
	}

	// 删除
	public static void singleNodeDelete() throws SolrServerException, IOException {
		String urlString = "http://172.17.149.196:8983/solr/techproducts";
		HttpSolrClient solr = new HttpSolrClient(urlString);

		UpdateResponse response = solr.deleteById("552199");

		System.out.println(response);

		// Remember to commit your changes!
		solr.commit();
		solr.close();
	}

	// SolrCloud client
	public static void solrCloudClient() throws SolrServerException, IOException {
		// 172.17.149.64:6181,172.17.149.64:6182,172.17.149.64:6182/slor2/bus1
		String zkHostString = "172.17.149.196:2181,172.17.149.196:2182,172.17.149.196:2183/slor2/bus1";
		CloudSolrClient solr = new CloudSolrClient(zkHostString);
		solr.setDefaultCollection("article");
		// 设置uniquekey的名字 默认是id
		solr.setIdField("id");
		ModifiableSolrParams params = new ModifiableSolrParams();
		params.set("q", "*:*");
		params.set("start", 0);
		params.set("rows", 20);

		// 排序
		params.set("sort", "create_time desc");

		QueryResponse response = solr.query(params);
		SolrDocumentList list = response.getResults();
		System.out.println(list.getNumFound());
		System.out.println(list.size());
		for (SolrDocument doc : list) {
			System.out.println(doc);
		}

		solr.close();
	}

	// SolrCloud client
	public static void solrCreateIndex() throws SolrServerException, IOException {
		// 172.17.149.64:6181,172.17.149.64:6182,172.17.149.64:6182/slor2/bus1
		
		CollectionAdminRequest.Create collectionAdminRequest = new CollectionAdminRequest.Create();
		collectionAdminRequest.setCollectionName("test");
		collectionAdminRequest.setConfigName("test");
		collectionAdminRequest.setNumShards(3);
		collectionAdminRequest.setReplicationFactor(1);
		collectionAdminRequest.setMaxShardsPerNode(3);
		
		String zkHostString = "172.17.149.196:2181,172.17.149.196:2182,172.17.149.196:2183";
		CloudSolrClient solr = new CloudSolrClient(zkHostString);
		
		collectionAdminRequest.process(solr);
		
	}
}
